import 'dart:math';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:extended_image/extended_image.dart';

import '../base_library.dart';

/// 支持本地和网络路径
class ImageView extends StatefulWidget {
  final String? url;
  final String? path;
  final File? file;
  final double? width;
  final double? height;
  final BoxFit? fit;
  final BorderRadius? borderRadius;
  final bool isCircular;

  const ImageView({
    Key? key,
    this.url,
    this.path,
    this.file,
    this.width,
    this.height,
    this.fit,
    this.borderRadius = BorderRadius.zero,
    this.isCircular = false,
  }) : super(key: key);

  factory ImageView.network({
    required String url,
    required double width,
    required double height,
    BoxFit? fit = BoxFit.cover,
    BorderRadius? borderRadius = BorderRadius.zero,
    bool isCircular = false,
  }) {
    return ImageView(
      url: url,
      width: width,
      height: height,
      fit: fit,
      borderRadius: borderRadius,
      isCircular: isCircular,
    );
  }

  factory ImageView.assets({
    required String path,
    required double width,
    required double height,
    BoxFit? fit = BoxFit.cover,
    BorderRadius? borderRadius = BorderRadius.zero,
    bool isCircular = false,
  }) {
    return ImageView(
      path: path,
      width: width,
      height: height,
      fit: fit,
      borderRadius: borderRadius,
      isCircular: isCircular,
    );
  }

  factory ImageView.file({
    required File file,
    required double width,
    required double height,
    BoxFit? fit = BoxFit.cover,
    BorderRadius? borderRadius = BorderRadius.zero,
    bool isCircular = false,
  }) {
    return ImageView(
      file: file,
      width: width,
      height: height,
      fit: fit,
      borderRadius: borderRadius,
      isCircular: isCircular,
    );
  }

  @override
  _ImageViewState createState() => _ImageViewState();
}

class _ImageViewState extends State<ImageView>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    _controller = AnimationController(
        vsync: this,
        duration: const Duration(seconds: 2),
        lowerBound: 0.0,
        upperBound: 1.0);
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var image;
    if (widget.url != null) {
      image = ExtendedImage.network(
        widget.url!,
        width: widget.width,
        height: widget.height,
        cache: true,
        borderRadius: widget.borderRadius ?? BorderRadius.circular(0.0),
        shape: widget.isCircular ? BoxShape.circle : BoxShape.rectangle,
        fit: widget.fit,
        loadStateChanged: (ExtendedImageState state) {
          switch (state.extendedImageLoadState) {
            case LoadState.loading:
              _controller.reset();
              return Container(
                width: widget.width,
                height: widget.height,
                alignment: Alignment.center,
                child: CupertinoActivityIndicator(
                  radius: 10,
                ),
              );
            case LoadState.completed:
              _controller.forward();
              return FadeTransition(
                opacity: _controller,
                child: ExtendedRawImage(
                  image: state.extendedImageInfo?.image,
                  fit: widget.fit,
                  width: widget.width,
                  height: widget.height,
                ),
              );
            default:
              _controller.reset();
              return GestureDetector(
                child: Stack(
                  fit: StackFit.expand,
                  children: <Widget>[
                    Image.asset(
                      BaseLibrary.resConfig.defaultImage,
                      fit: widget.fit,
                    ),
                  ],
                ),
                onTap: () {
                  state.reLoadImage();
                },
              );
          }
        },
      );
    } else if (widget.path != null) {
      image = ExtendedImage.asset(
        widget.path!,
        width: widget.width,
        height: widget.height,
        fit: widget.fit,
      );
    } else {
      image = ExtendedImage.file(
        widget.file!,
        width: widget.width,
        height: widget.height,
        fit: widget.fit,
      );
    }

    return widget.isCircular
        ? ClipOval(
            child: image,
          )
        : ClipRRect(
            borderRadius: widget.borderRadius,
            child: image,
          );
  }
}
